package com.xxl.job.admin.core.conf;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author pan.shun
 * 自定义一个数据库标示提供类，实现DatabaseIdProvider接口
 */
@Component
public class MyDatabaseIdProvider implements DatabaseIdProvider {
    @Override
    public void setProperties(Properties p) {
    }

    @Override
    public String getDatabaseId(DataSource dataSource) throws SQLException {
        Connection conn = dataSource.getConnection();
        String dbName = conn.getMetaData().getDatabaseProductName();
        String dbAlias = "";
        switch (dbName) {
            case "MySQL":
                dbAlias = "mysql";
                break;
            case "Oracle":
                dbAlias = "oracle";
                break;
            case "PostgreSQL":
                dbAlias = "postgres";
            case "SQLite":
                dbAlias = "sqlite";
            default:
                break;
        }
        return dbAlias;
    }
}